package com.entertainment.ticketing.controller;

import com.entertainment.ticketing.dto.EventInfoDTO;
import com.entertainment.ticketing.dto.TicketInfoDTO;
import com.entertainment.ticketing.dto.EventTicketsBatchUpdateRequest;
import com.entertainment.ticketing.dto.PageResponse;
import com.entertainment.ticketing.service.EventService;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import jakarta.validation.Valid;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/events")
public class EventController {
    private final EventService eventService;

    public EventController(EventService eventService) {
        this.eventService = eventService;
    }

    @GetMapping
    public ResponseEntity<PageResponse<EventInfoDTO>> listEvents(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "status", required = false) Byte status,
            @RequestParam(value = "startFrom", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startFrom,
            @RequestParam(value = "startTo", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTo,
            @RequestParam(value = "page", defaultValue = "0") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "sortBy", defaultValue = "startTime") String sortBy,
            @RequestParam(value = "order", defaultValue = "ASC") String order
    ) {
        Sort.Direction direction = "DESC".equalsIgnoreCase(order) ? Sort.Direction.DESC : Sort.Direction.ASC;
        PageResponse<EventInfoDTO> result = eventService.search(
                keyword, category, status, startFrom, startTo,
                page, size, sortBy, direction
        );
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{id}")
    public ResponseEntity<EventInfoDTO> getEvent(@PathVariable Long id) {
        return ResponseEntity.ok(eventService.getById(id));
    }

    @GetMapping("/recent")
    public ResponseEntity<List<EventInfoDTO>> recentEvents(@RequestParam(value = "size", defaultValue = "8") int size) {
        return ResponseEntity.ok(eventService.recent(size));
    }

    // 管理员：新增/更新/删除
    @PostMapping
    public ResponseEntity<EventInfoDTO> createEvent(@Valid @RequestBody EventInfoDTO req) {
        return ResponseEntity.ok(eventService.create(req));
    }

    @PutMapping("/{id}")
    public ResponseEntity<EventInfoDTO> updateEvent(@PathVariable Long id, @RequestBody EventInfoDTO req) {
        return ResponseEntity.ok(eventService.update(id, req));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteEvent(@PathVariable Long id) {
        eventService.delete(id);
        return ResponseEntity.ok().build();
    }

    // 管理员：上传海报文件，更新并返回最新活动信息
    @PostMapping("/{id}/poster")
    public ResponseEntity<EventInfoDTO> uploadPoster(
            @PathVariable Long id,
            @org.springframework.web.bind.annotation.RequestParam("file") MultipartFile file
    ) {
        return ResponseEntity.ok(eventService.uploadPoster(id, file));
    }

    // 管理员：票务批量联动编辑（新增/更新/删除），返回最新票务列表
    @PutMapping("/{id}/tickets/batch")
    public ResponseEntity<java.util.List<TicketInfoDTO>> batchUpdateTickets(
            @PathVariable Long id,
            @jakarta.validation.Valid @RequestBody EventTicketsBatchUpdateRequest req
    ) {
        return ResponseEntity.ok(eventService.updateTicketsBatch(id, req));
    }
}